package org.microblog.service.impl;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.microblog.dao.CollectionDao;

import org.microblog.dao.LikesDao;
import org.microblog.entity.Blog;
import org.microblog.entity.BlogVo;
import org.microblog.entity.Collection;
import org.microblog.entity.CommentVo;
import org.microblog.service.CollectionService;
import org.microblog.util.Page;
import org.microblog.util.SessionFatoryUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class CollectionServiceImpl implements CollectionService {
    SqlSessionFactory sessionFactory = SessionFatoryUtil.sessionFactory;
    SqlSession sqlSession = sessionFactory.openSession();
    BlogVo vo=new BlogVo();
    @Override
    public int getCollectionCountByBid(int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        int ret=dao.getCollectionCountByBid(bid);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public int addCollection(Collection collection) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        int ret=dao.addCollection(collection);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public Collection findCollection(int uid, int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        sqlSession.commit();
        sqlSession.close();
        Collection collection=dao.findCollection(uid, bid);
        return collection;
    }

    @Override
    public int deleteCollection(Collection collection) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        int ret=dao.deleteCollection(collection);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public int deleteCollectionByBid(int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        sqlSession.commit();
        sqlSession.close();
        int ret=dao.deleteCollectionByBid(bid);
        return ret;
    }

    @Override
    public void getCollectionsByPage(int uid, String keyword, Page pageObj) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession(true);
        vo.setuId(uid);
        vo.setKeyword(keyword);
        vo.setPageNo(pageObj.getBeginIndex());
        vo.setPageSize(pageObj.getPageSize());

        CollectionDao dao = sqlSession.getMapper(CollectionDao.class);
        int totalCount = dao.getCollectionCountByUidAndKeyword(uid, keyword);
        pageObj.setTotalCount(totalCount);
        if (totalCount > 0) {
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Blog> blogList = dao.getCollectionsByPage(vo);
            pageObj.setBlogsList(blogList);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setBlogsList(new ArrayList<>());
        }
        sqlSession.commit();
        sqlSession.close();
    }
}
